﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RegisterationCommon.Student;
using System.Data;
using RegisterationCommon.Common;
using RegisterationCommon.School;
using RegisterationCommon.Education;
using System.IO;
namespace RegisterationSystem.Space_School
{
    public partial class students_audited : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Form["action"] != "")
                {
                    switch (Request.Form["action"])
                    {
                        case "updstustatus":
                            UpdStustatus();
                            break;
                        case "pswdreset":
                            UpdStudentPswdReset();
                            break;
                        //case "derive_excelList":
                        //    derive_excelList();
                        //    break;
                        //case "deriveexcelListAll":
                        //    derive_excelListAll();
                        //    break;
                    }
                }
                Bind();
            }
        }
        //绑定学校
        void Bind()
        {
            //DataTable dt = DBAction.GetDTFromSQL("select * from SchoolInfo where id =" + SchoolAccount.Current.SchID + "");
            DataTable dt = DBAction.GetDTFromSQL("select * from SchoolInfo where id =" + Cookies.Get("SchID") + "");
            if (dt.Rows.Count > 0)
            {
                ListItem item = new ListItem();
                item.Text += dt.Rows[0]["Name"].ToString();
                item.Value = dt.Rows[0]["id"].ToString();
                ddlSchoolInfo.Items.Add(item);
            }
        }
        //重置学生帐号密码
        void UpdStudentPswdReset()
        {
            string id = Request.Form["id"].ToString();
            string StudentPswd = Request.Form["StudentPswd"].ToString();
            StudentInfo s = new StudentInfo();
            s.MdfAccountPwd(Convert.ToInt64(id),BaseLibrary.ConvertToMD5(StudentPswd.ToLower()));
        }
        //修改回访状态
        void UpdStustatus()
        {
            string id = Request.Form["id"];
            string status = Request.Form["status"];
            string phtxt = Request.Form["phtxt"];
            string stainfo = Request.Form["stainfo"];

            DataTable dt = DBAction.GetDTFromSQL("select Level from Students where ID =" +Convert.ToInt32(id.ToString()));
            if (stainfo.ToString() == "1")
            {
                if (dt.Rows[0]["Level"].ToString() == "3")
                {
                    Response.Clear();
                    Response.Write("-1n");
                    Response.End();
                    return;
                }
            }
                StudentInfo s = new StudentInfo();
                s.ID = Convert.ToInt32(id);
                s.PhoneStatus = Convert.ToBoolean(Convert.ToInt32(status));
                s.PhoneStatusText = phtxt;
                s.Status = Convert.ToInt32(stainfo);
                s.SetPhoneStatusText();
                s.MdfStatus();
        }

        //void DirectoryInfoList()
        //{
        //    int day = 0;
        //    DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath(@"..\StudentInfoToExcel\"));
        //    FileInfo[] dirInfo = theFolder.GetFiles();
        //    //遍历文件夹
        //    foreach (FileInfo NextFile in dirInfo)
        //    {
        //        string filename = NextFile.Name;
        //        string[] Date = filename.Split('_');
        //        if (Date.Length > 1)
        //        {
        //            string strTimeNow = DateTime.Now.ToString("yyyyMMdd");
        //            int d1 = int.Parse(Date[1]);
        //            int d2 = int.Parse(strTimeNow);
        //            int ND = d2 - d1;
        //            day = ND;
        //            if (day >= 1)
        //            {
        //                File.Delete(Server.MapPath(@"..\StudentInfoToExcel\" + filename));
        //            }
        //        }
        //    }
        //}
        //void derive_excelList()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string stuID = "";
        //    string idlist = Request.Form["id"];
        //    string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //    for (int i = 0; i < ids.Length; i++)
        //    {
        //        if (i != (ids.Length - 1))
        //        {
        //            stuID = stuID + ids[i] + " or id=";
        //        }
        //        else
        //        {
        //            stuID = stuID + ids[i];
        //        }
        //    }
        //    DataTable dt = DBAction.GetDTFromSQL("Select * From " + ETableName.Students + " where id=" + stuID);
        //    ExcelHelper excel = new ExcelHelper();
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //    Response.Clear();
        //    string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //    Response.Write(result);
        //    Response.End();
        //}



        //void derive_excelListAll()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string where = Request.Form["where"];
        //    string typeid = Request.Form["typeid"];
        //    ExcelHelper excel = new ExcelHelper();
        //    DataTable dt = excel.GetStudentBySchoolID(where, typeid);
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //        //string[] Toexcelpathlist = Toexcelpath.Split('\\');
        //        Response.Clear();
        //        //string result = "..\\" + Toexcelpathlist[4] + "\\" + Toexcelpathlist[5];
        //        string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //        Response.Write(result);
        //        Response.End();
        //}
    }
}